﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Delonix_Regia_Hotel_Management_System.Class;
using System.Data.SqlClient;
using System.Configuration;

namespace Delonix_Regia_Hotel_Management_System
{
    public partial class Update_And_Delete_of_Staff : Form
    {
        SqlConnection conn = new SqlConnection();
        
        SqlCommand comm = new SqlCommand();
        public Update_And_Delete_of_Staff()
        {
            InitializeComponent();
        }

       
            

        private void button1_Click(object sender, EventArgs e)
        {
            if (LogInClass.position == "Administrator")
            {
                Main__Management_and_Administrator_ mainA = new Main__Management_and_Administrator_();
                this.Hide();
                mainA.Show();
            }
            else
            {
                Main__Receptionist_ mainR = new Main__Receptionist_();
                this.Hide();
                mainR.Show();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form1 F = new Form1();
            this.Hide();
            F.Show(); 
        }

        private void btnRetrieveAll_Click(object sender, EventArgs e)
        {
            List<Staff> staff = DBManager.getAllStaff();
            dataGridView1.DataSource = staff;
            
            if (staff.Count > 0)
            {

                dataGridView1.Columns["bankAccount"].Visible = false;
                dataGridView1.Columns["staffPassword"].Visible = false;
            }
            
        }

        private void Bind()
        {
            conn.ConnectionString = "data source=localhost; initial catalog=DelonixRegia; user id=user; password=Password_2013";
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * Staff", conn);
            //DataTable dt = new DataTable();
            //da.Fill(dt);
            dataGridView1.DataSource = da;
            conn.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            conn.ConnectionString = "data source=localhost; initial catalog=DelonixRegia; user id=user; password=Password_2013";
            conn.Open();

            for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            {

                SqlCommand cmd = new SqlCommand("Update Staff set staffId=@staffId,staffName=@staffName, homeAddress=@homeAddress, phoneNum=@phoneNum, position=@position Where(staffId=@staffId)", conn);
                cmd.Parameters.AddWithValue("@staffId", dataGridView1.Rows[i].Cells[0].Value.ToString());
                cmd.Parameters.AddWithValue("@staffName", dataGridView1.Rows[i].Cells[1].Value.ToString());
                cmd.Parameters.AddWithValue("@homeAddress", dataGridView1.Rows[i].Cells[4].Value.ToString());
                cmd.Parameters.AddWithValue("@phoneNum", dataGridView1.Rows[i].Cells[5].Value.ToString());
                cmd.Parameters.AddWithValue("@position", dataGridView1.Rows[i].Cells[6].Value.ToString());
                cmd.ExecuteNonQuery();

            }
            conn.Close();
            MessageBox.Show("updated");
            Bind();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you really want to delete selected record(s)?",
                "Delete warning", MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, false)
                == DialogResult.Yes)
            {
                conn.ConnectionString = "data source=localhost; initial catalog=DelonixRegia; user id=user; password=Password_2013";
                SqlCommand delcmd = new SqlCommand();
                string staffID = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                if (dataGridView1.Rows.Count > 1)
                {
                    delcmd.CommandText = "DELETE FROM Staff WHERE staffId=@staffID";
                    conn.Open();
                    delcmd.Connection = conn;
                    delcmd.Parameters.AddWithValue("@staffId", staffID);
                    delcmd.ExecuteNonQuery();
                    conn.Close();
                    MessageBox.Show("Row Deleted");
                }
                Bind();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            Staff staff = new Staff();
            string staffId = tbxStaffId.Text;
            if (DBManager.checkStaffId(staffId))
            {
                staff = DBManager.GetStaffID(staffId);
                BindingSource binding = new BindingSource();
                binding.DataSource = staff;
                dataGridView1.DataSource = binding;

                dataGridView1.Columns["bankAccount"].Visible = false;
            }
            else
            {
                MessageBox.Show("The staff id cannot be found");
            }
        }


       
      

      
    }
}
